//
//  SYModifyPwdViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/8/3.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "SYModifyPwdViewController.h"


@interface SYModifyPwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oriPassword;  //原始密码
@property (weak, nonatomic) IBOutlet UITextField *rePassword;   //重复密码
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;      //完成按钮


@end

@implementation SYModifyPwdViewController

- (void)loadView {
    
    [super loadView];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 30, 25)];
    [leftButton setImage:[UIImage imageNamed:@"listmore_arrowleft"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
}

- (void)backButtonTapped:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    _phoneLabel.text = [NSString stringWithFormat:@"手机号码  +86 %@",_phoneNum];
    _confirmBtn.layer.cornerRadius = 4;
    
    self.title = @"设置新密码";
    
    //设置相关控件的属性
    [self configureSubViews];
    
}

-(void)configureSubViews{
    
    //textField
    [_oriPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_rePassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}

-(void)textFieldDidChange:(UITextField *)field{
    
    
    if (field==_oriPassword) {
        if (_oriPassword.text.length > 16) {
            UITextRange *markedRange = [_oriPassword markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [_oriPassword.text rangeOfComposedCharacterSequenceAtIndex:16];
            _oriPassword.text = [_oriPassword.text substringToIndex:range.location];
        }
    }else if (field ==_rePassword) {
        if (_rePassword.text.length >16) {
            UITextRange *markedRange = [_rePassword markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [_rePassword.text rangeOfComposedCharacterSequenceAtIndex:16];
            _rePassword.text = [_rePassword.text substringToIndex:range.location];
        }
    }
    
    if (_oriPassword.text.length >=6&&_oriPassword.text.length <=16 && _rePassword.text.length >= 6&& _rePassword.text.length <= 16) {
        
        [_confirmBtn setBackgroundColor:colorFromHex(@"fb524a")];
        _confirmBtn.enabled = YES;
        
    }else{
        
        [_confirmBtn setBackgroundColor:colorFromHex(@"fccaca")];
        _confirmBtn.enabled = NO;
    }
    
}

#pragma mark -完成事件
- (IBAction)confirmAction:(id)sender {
    
    if ([[_oriPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        [Utils alertViewWithMessage:@"请输入密码"];
        return;
    }
    
    if ([[_oriPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        [Utils alertViewWithMessage:@"请输入确认密码"];
        return;
    }
    
    if (_oriPassword.text.length<6 ||_oriPassword.text.length>16) {
        
        [Utils alertViewWithMessage:@"请将密码限制在6-16位以内"];
        return;
    }
    
    if (_rePassword.text.length<6 ||_rePassword.text.length>16) {
        
        [Utils alertViewWithMessage:@"请将密码限制在6-16位以内"];
        return;
    }
    
    if (![_oriPassword.text isEqualToString:_rePassword.text]) {
        
        [Utils alertViewWithMessage:@"两次密码输入不一致,请重新输入确认密码"];
        return;
    }
    
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setObject:@"api_user_repass" forKey:@"method"];
    [dict setObject:_phoneNum forKey:@"Mobile"];
    [dict setObject:_verifyStr forKey:@"verify"];
    [dict setObject:[Utils MD5EncryptWith:_oriPassword.text] forKey:@"newpass"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:self->_phoneNum forKey:@"_phoneNum"];
        [dic setObject:self->_oriPassword.text forKey:@"newpass"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SY_verifyDoLogin" object:dic];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.description];
    }];
    
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
